package cn.doctorpda.conference.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.bean.search.SearchConItem;
import cn.doctorpda.conference.utils.ImageUtils;
import cn.doctorpda.conference.utils.L;
import cn.doctorpda.conference.utils.ScreenUtils;

/**
 * Created by huangjinlong on 2016/3/29.
 */
public class SearchConAdapter extends RecyclerView.Adapter<SearchConAdapter.ViewHolder> {

    //    private List<String> datas = new ArrayList<>();
    private ArrayList<SearchConItem> infoList = new ArrayList<SearchConItem>();
    private OnItemClickListener listener;
    private Context context;
    private String isHome = "";

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, SearchConItem data);
    }

    public SearchConAdapter() {

    }

    public SearchConAdapter(String isHome) {
        this.isHome = isHome;
    }

    public void clear() {
        this.infoList.clear();
        notifyDataSetChanged();
    }

    public void addAll(ArrayList<SearchConItem> infoList) {
        this.infoList.addAll(infoList);
        notifyDataSetChanged();
    }

    public void add(String data) {
//        this.datas.add(2, data);
        notifyItemInserted(2);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_check_meeting_new, parent, false);
        LinearLayout.LayoutParams la = new LinearLayout.LayoutParams(ScreenUtils.getScreenWidth(parent.getContext()) / 2, ScreenUtils.getScreenHeight(parent.getContext()) / 4);
        view.setLayoutParams(la);
        this.context = parent.getContext();
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final int pos = position;
        final SearchConItem itemInfo = infoList.get(pos);
//        ImageUtils.loadImage(context, itemInfo.getUrl(), holder.imvMeetingPic);
//        T.showShort(context,itemInfo.getCity_s());
        if (itemInfo.getCity_s() == null || itemInfo.getCity_s().isEmpty()) {
            itemInfo.setCity_s("");
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String date_start = "", date_stop = "";
        if (!TextUtils.isEmpty(itemInfo.getStart_at()) && itemInfo.getStart_at().length() > 10) {
            try {
                date_start = sdf.format(Date.valueOf(itemInfo.getStart_at().substring(0, 9)));
                holder.tvMeetingDate.setText(date_start);
            } catch (Exception e) {
                e.printStackTrace();
                holder.tvMeetingDate.setText(itemInfo.getStart_at());
            }
        } else {
            holder.tvMeetingDate.setText("待确定");
        }
        SimpleDateFormat compare = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if (compare.parse(itemInfo.getEnd_at()).after(new java.util.Date())) {
                L.d(compare.parse(itemInfo.getEnd_at()).toString());
                holder.tvMeetingState.setText("报名中");
                holder.tvMeetingState.setBackgroundResource(R.drawable.btn_item_meeting_details);
            } else {
                holder.tvMeetingState.setText("报名截止");
                holder.tvMeetingState.setBackgroundResource(R.drawable.btn_item_meeting_state);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }


        holder.tvMeetingAddress.setText(itemInfo.getProvice_s() + itemInfo.getCity_s());
        try {
            holder.tvMeetingTitle.setText(Html.fromHtml(itemInfo.getTitle()).toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(position, infoList.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (isHome.equals("home")) {
            if (infoList.size() > 2) {
                return 2;
            }
        }
        return infoList.size();
    }

    //item元素
    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvMeetingTitle, tvMeetingState, tvMeetingDate, tvMeetingAddress;//会议标题，会议状态，会议时间，会议地址
//        ImageView imvMeetingPic;//会议图片

        public ViewHolder(View itemView) {
            super(itemView);
            tvMeetingTitle = (TextView) itemView.findViewById(R.id.txt_item_title);
            tvMeetingState = (TextView) itemView.findViewById(R.id.txt_item_meeting_state);
            tvMeetingDate = (TextView) itemView.findViewById(R.id.txt_item_date);
            tvMeetingAddress = (TextView) itemView.findViewById(R.id.txt_item_address);

//            imvMeetingPic = (ImageView) itemView.findViewById(R.id.imv_meeting);
        }
    }
}
